package com.wwj.core.system.service.impl;

import com.wwj.core.system.entity.Menu;
import com.wwj.core.system.entity.mapper.MenuMapper;
import com.wwj.core.system.entity.mapper.UserRoleMenuMapper;
import com.wwj.core.system.service.IMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xya on 16-10-24.
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private UserRoleMenuMapper userRoleMenuMapper;

    @Override
    public Set<String> getPermissionsAsString(String userId) {
        List<Menu> menuList = userRoleMenuMapper.selectMenuByUser(userId);
        if (CollectionUtils.isEmpty(menuList)) {
            return null;
        }
        Set<String> set = new HashSet<String>();
        for (Menu m : menuList) {
            if (StringUtils.isBlank(m.getPageUrl())) {
                continue;
            }
            set.add(m.getPageUrl());
        }
        return set;
    }

    @Override
    public List<Map<String, Object>> findMenuByUser(String userId) throws Exception {
        List<Map<String, Object>> nodes = new ArrayList<Map<String, Object>>();
        List<Menu> menuList = userRoleMenuMapper.selectMenuByUser(userId);
        for (Menu menu : menuList) {
            Map<String, Object> node = new HashMap<String, Object>();
            Map<String, Object> attributes = new HashMap<String, Object>();
            node.put("code", menu.getCode());
            node.put("name", menu.getName());
            if (!StringUtils.isBlank(menu.getPcode())) {
                node.put("pcode", menu.getPcode().toString());
            } else {
                node.put("pcode", "");
            }
            node.put("text", menu.getName());
            if (!StringUtils.isBlank(menu.getPageUrl())) {
                node.put("url", menu.getPageUrl());
                //attributes.put("url",menu.getPageurl());
            } else {
                node.put("url", "");
                //attributes.put("url","");
            }
            node.put("icon", menu.getIcon());
            node.put("attributes", attributes);
            nodes.add(node);
        }
        //转树形结构
        nodes = this.toTree(nodes);
        return nodes;
    }

    @Override
    public List<Menu> findAllMenu() throws Exception {
        return menuMapper.select();
    }

    private List<Map<String, Object>> toTree(List<Map<String, Object>> list) {
        List<Map<String, Object>> _list = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> _list1 = new ArrayList<Map<String, Object>>(list);
        List<Map<String, Object>> _list2 = new ArrayList<Map<String, Object>>(list);
        List<Map<String, Object>> _temp = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : _list1) {
            String pcode = (String) map.get("pcode");
            if (StringUtils.isBlank(pcode)) {
                _temp.add(map);
                _list.add(map);
            }
        }
        for (Map<String, Object> map : _temp) {
            _list1.remove(map);
        }
        return this.toTree2(_list, _list2);
    }

    @SuppressWarnings("unchecked")
    private List<Map<String, Object>> toTree2(List<Map<String, Object>> mainList, List<Map<String, Object>> list) {
        List<Map<String, Object>> _list1 = new ArrayList<Map<String, Object>>(list);
        List<Map<String, Object>> _temp = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : mainList) {
            String code = (String) map.get("code");
            for (Map<String, Object> map2 : list) {
                String pcode = (String) map2.get("pcode");
                if (StringUtils.isNotBlank(pcode) && code.equals(pcode)) {

                    List<Map<String, Object>> childList = null;
                    if (map.get("children") != null) {
                        childList = (List<Map<String, Object>>) map.get("children");
                    } else {
                        childList = new ArrayList<Map<String, Object>>();
                    }
                    childList.add(map2);
                    map.put("children", childList);
                    _temp.add(map2);
                }
            }
            for (Map<String, Object> map3 : _temp) {
                _list1.remove(map3);
            }
            if (map.get("children") != null) {
                toTree2((List<Map<String, Object>>) map.get("children"), list);
            }
        }
        return mainList;
    }
}
